/**
 * Project Name:YCApiServer-Api
 * File Name:WorkCommentService.java
 * Package Name:com.yc.music.service.service103
 * Date:2016年5月8日下午5:47:11
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service103;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.alibaba.fastjson.JSONObject;
import com.yc.music.common.context.YcContext;
import com.yc.music.common.push.YcPush;
import com.yc.music.common.util.BindingResultUtil;
import com.yc.music.common.util.RoomUtil;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.cache.UserCacheMapper;
import com.yc.music.mapper.cache.WorkCacheMapper;
import com.yc.music.mapper.dao.LyricsMapper;
import com.yc.music.mapper.dao.PushMessageFlowMapper;
import com.yc.music.mapper.dao.WorkCommentMapper;
import com.yc.music.mapper.dao.WorkMapper;
import com.yc.music.model.YcPushMassageFlow;
import com.yc.music.model.comment.YcComment;
import com.yc.music.model.musicLyrics.MusicLyrics;
import com.yc.music.model.musicWork.WorkOpenDetail;
import com.yc.music.model.musicuser.MusicUserVo;

import cn.jpush.api.push.PushResult;

/**
 * 评论 service ClassName: CommentService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年5月8日 下午5:47:11 <br/>
 *
 * @author panguixiang
 * @version
 */
@Service
public class CommentService {
	
	private static Logger logger = Logger.getLogger(CommentService.class);

	@Autowired
	private WorkCommentMapper workCommentMapper;
	
	@Autowired
	private UserCacheMapper userCacheMapper;
	
	@Autowired
	private WorkCacheMapper workCacheMapper;
	
	@Autowired
	private LyricsMapper lyricsMapper;
	
	@Autowired
	private WorkMapper workMapper;
	
	@Autowired
	private PushMessageFlowMapper pushMessageFlowMapper;
	
	@Value("${Pic_QiLiu}")
	private String pIC_QiLiu;
	
	/**
	 * 
	 * commentList:(分页获得歌曲/歌词评论列表). <br/>
	 *
	 * @author panguixiang
	 * @param itemid
	 * @param type 1=歌曲评论，2=歌词评论
	 * @param page
	 * @return
	 * @since JDK 1.7
	 */
	public void commentList(Long itemid, Integer page, Map<String, Object> result,Integer type) throws Exception {
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		int start = 0, size = 20;
		if (page!=null && page > 1) {
			start = (page - 1) * 20;
		}
		List<YcComment> list = workCommentMapper.workCommentList(itemid,type, start, size);
		MusicUserVo user = null;
		if(list!=null) {
			for(YcComment comment:list) {
				user=userCacheMapper.getCacheUservo(comment.getUid());
				if(user!=null){
					comment.setHeaderurl(RoomUtil.jointDomain(user.getHeadurl(),pIC_QiLiu));
					comment.setNickname(user.getNickname());
				}
				if(comment.getTarget_uid()!=0){
					user=userCacheMapper.getCacheUservo(comment.getTarget_uid());
					if(user!=null){
						comment.setTargetheaderurl(RoomUtil.jointDomain(user.getHeadurl(),pIC_QiLiu));
						comment.setTarget_nickname(user.getNickname());
					}
				}
			}
		}
		result.put(YcContext.DATA, list);
	}
	
	/**
	 * 
	 * geCcommentList:复制获取评论列表数据接口，提供活动调用. <br/> 
	 * @param itemid
	 * @param page
	 * @param result
	 * @param type
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public List<YcComment> geCcommentList(Long itemid, Integer page, Map<String, Object> result,Integer type) throws Exception {
		int start = 0, size = 20;
		if (page!=null && page > 1) {
			start = (page - 1) * 20;
		}
		List<YcComment> list = workCommentMapper.workCommentList(itemid,type, start, size);
		MusicUserVo user = null;
		if(list!=null) {
			for(YcComment comment:list) {
				user=userCacheMapper.getCacheUservo(comment.getUid());
				if(user!=null){
					comment.setHeaderurl(RoomUtil.jointDomain(user.getHeadurl(),pIC_QiLiu));
					comment.setNickname(user.getNickname());
				}
				if(comment.getTarget_uid()!=0){
					user=userCacheMapper.getCacheUservo(comment.getTarget_uid());
					if(user!=null){
						comment.setTargetheaderurl(RoomUtil.jointDomain(user.getHeadurl(),pIC_QiLiu));
						comment.setTarget_nickname(user.getNickname());
					}
				}
			}
		}
		return list;
	}
	
	/**
	 * 
	 * saveComment:(发布歌曲、歌词 评论). <br/> 
	 *
	 * @author panguixiang
	 * @param YcComment
	 * @param binding
	 * @param type 1=歌曲评论，2=歌词评论
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String, Object> saveComment(YcComment comment, BindingResult binding,
			Map<String, Object> result)
			throws Exception {

		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if (StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		if(comment.getComment_type()==2 && comment.getTarget_uid()==0l) {//1=默认，2=跟帖
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.COMMENT_TARGET_USERID_NULL);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		workCommentMapper.saveWorkComment(comment);
		if(comment.getType()==1) {
			workMapper.addCommentNum(comment.getItemid(),1);//加1
		} else {
			lyricsMapper.addLrycCommentNum(comment.getItemid(),1);//加1
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, comment.getId());
		return result;
	}
	
	
	/**
	 * 
	 * saveComment:(发布歌曲、歌词 评论,增加推送). <br/> 
	 *
	 * @author qinhaiguan
	 * @param YcComment
	 * @param binding
	 * @param type 1=歌曲评论，2=歌词评论
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 * @version V2.2
	 */
	public Map<String, Object> saveComment22(YcComment comment, BindingResult binding,
			Map<String, Object> result)
			throws Exception {

		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if (StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		if(comment.getComment_type()==2 && comment.getTarget_uid()==0l) {//1=默认，2=跟帖
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.COMMENT_TARGET_USERID_NULL);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		workCommentMapper.saveWorkComment(comment);
		if(comment.getType()==1) {
			workMapper.addCommentNum(comment.getItemid(),1);//加1
		} else {
			lyricsMapper.addLrycCommentNum(comment.getItemid(),1);//加1
		}
		
		//***********增加推送部分****************
		
		
		Map<String,Object> param = new HashMap<String,Object>();
		//记录推送流水
		YcPushMassageFlow flow =new YcPushMassageFlow();
		
		//类型，1=歌曲，2=歌词
		if (comment.getType()==1) {
			//如果comment.getTarget_uid()不为空则针对人进行评论，向被评论的人进行推送
			if (comment.getTarget_uid()>0) {
				//获取歌曲作品名 
//				WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(comment.getItemid());
				//通过用户id获取用户名
				MusicUserVo userVo=userCacheMapper.getCacheUservo(comment.getUid());
				logger.info("开始推送评论消息>>>>>>推送给用户id:"+comment.getTarget_uid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment());
				param.put("type", YtConstant.PUSH_TYPE_COMMENT);
				param.put("title", YtConstant.YINCHAO);
				param.put("content", YtConstant.PUSH_TITLE_COMMENT_USER.replaceAll("U", userVo.getNickname()));
				String pushMessage=JSONObject.toJSONString(param);
				
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(comment.getTarget_uid()), pushMessage);
				logger.info("推送评论消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(comment.getTarget_uid()), pushMessage);
				logger.info("推送评论消息返回结果IOS:"+reIOS);
				
				String msgIdAnd=null;
				String msgIdIOS=null;
				
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setWorktype(1);
				flow.setUid(comment.getUid());
				flow.setOtheruid(comment.getTarget_uid());
				
			}else{//如果comment.getTarget_uid()为空则针对作品进行评论，向被作品的作者进行推送
				//获取歌曲作品名 
				WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(comment.getItemid());
				//通过用户id获取用户名
				MusicUserVo userVo=userCacheMapper.getCacheUservo(comment.getUid());
				logger.info("开始推送评论消息>>>>>>推送给用户id:"+work.getUid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment());
				param.put("type", YtConstant.PUSH_TYPE_COMMENT);
				param.put("title", YtConstant.YINCHAO);
				param.put("content", YtConstant.PUSH_TITLE_COMMENT.replaceAll("U", userVo.getNickname())
						.replaceAll("W",work.getTitle()).replaceAll("C",comment.getComment()));
				String pushMessage=JSONObject.toJSONString(param);
				
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(work.getUid()), pushMessage);
				logger.info("推送评论消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(work.getUid()), pushMessage);
				logger.info("推送评论消息返回结果IOS:"+reIOS);
				
				
				String msgIdAnd=null;
				String msgIdIOS=null;
				
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setWorktype(1);
				flow.setUid(comment.getUid());
				flow.setOtheruid(work.getUid());
			}
			
//			//获取歌曲作品名 
//			WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(comment.getItemid());
//			//通过用户id获取用户名
//			MusicUserVo userVo=userCacheMapper.getCacheUservo(comment.getUid());
//			logger.info("开始推送评论消息>>>>>>推送给用户id:"+work.getUid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment());
//			param.put("type", YtConstant.PUSH_TYPE_COMMENT);
//			param.put("title", YtConstant.YINCHAO);
//			param.put("content", YtConstant.PUSH_TITLE_COMMENT.replaceAll("U", userVo.getNickname())
//					.replaceAll("W",work.getTitle()).replaceAll("C",comment.getComment()));
//			String pushMessage=JSONObject.toJSONString(param);
//			
//			
//			PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(work.getUid()), pushMessage);
//			logger.info("推送评论消息返回结果Android:"+reAnd);
//			PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(work.getUid()), pushMessage);
//			logger.info("推送评论消息返回结果IOS:"+reIOS);
			
		}else{
			//如果comment.getTarget_uid()不为空则针对人进行评论，向被评论的人进行推送
			if (comment.getTarget_uid()>0) {
				//获取歌词作品名 
//				MusicLyrics  lyrics=lyricsMapper.getMusicLyricsById(comment.getItemid());
				//通过用户id获取用户名
				MusicUserVo userVo=userCacheMapper.getCacheUservo(comment.getUid());
				
				logger.info("开始推送评论消息>>>>>>推送给用户id:"+comment.getTarget_uid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment());
				
				param.put("type", YtConstant.PUSH_TYPE_COMMENT);
				param.put("title", YtConstant.YINCHAO);
				param.put("content", YtConstant.PUSH_TITLE_COMMENT_USER.replaceAll("U", userVo.getNickname()));
				String pushMessage=JSONObject.toJSONString(param);
				
				String msgIdAnd=null;
				String msgIdIOS=null;
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(comment.getTarget_uid()), pushMessage);
				logger.info("推送评论消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(comment.getTarget_uid()), pushMessage);
				logger.info("推送评论消息返回结果IOS:"+reIOS);
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setWorktype(2);
				flow.setUid(comment.getUid());
				flow.setOtheruid(comment.getTarget_uid());
			}else{
				//获取歌词作品名 
				MusicLyrics  lyrics=lyricsMapper.getMusicLyricsById(comment.getItemid());
				//通过用户id获取用户名
				MusicUserVo userVo=userCacheMapper.getCacheUservo(comment.getUid());
				
				logger.info("开始推送评论消息>>>>>>推送给用户id:"+lyrics.getUid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment()+",comment.getUid()="+comment.getUid());
				
				param.put("type", YtConstant.PUSH_TYPE_COMMENT);
				param.put("title", YtConstant.YINCHAO);
				param.put("content", YtConstant.PUSH_TITLE_COMMENT.replaceAll("U", userVo.getNickname())
						.replaceAll("W",lyrics.getTitle()).replaceAll("C", comment.getComment()));
				String pushMessage=JSONObject.toJSONString(param);
				
				
				String msgIdAnd=null;
				String msgIdIOS=null;
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(lyrics.getUid()), pushMessage);
				logger.info("推送评论消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(lyrics.getUid()), pushMessage);
				logger.info("推送评论消息返回结果IOS:"+reIOS);
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setWorktype(2);
				flow.setUid(comment.getUid());
				flow.setOtheruid(lyrics.getUid());
			}
//			//获取歌词作品名 
//			MusicLyrics  lyrics=lyricsMapper.getMusicLyricsById(comment.getItemid());
//			//通过用户id获取用户名
//			MusicUserVo userVo=userCacheMapper.getCacheUservo(lyrics.getUid());
//			
//			logger.info("开始推送评论消息>>>>>>推送给用户id:"+lyrics.getUid()+",用户名:"+userVo.getNickname()+",推送的评论内容:"+comment.getComment());
//			
//			param.put("type", YtConstant.PUSH_TYPE_COMMENT);
//			param.put("title", YtConstant.YINCHAO);
//			param.put("content", YtConstant.PUSH_TITLE_COMMENT.replaceAll("U", userVo.getNickname())
//					.replaceAll("W",lyrics.getTitle()).replaceAll("C", comment.getComment()));
//			String pushMessage=JSONObject.toJSONString(param);
//			
//			
//			String msgIdAnd=null;
//			String msgIdIOS=null;
//			PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(comment.getTarget_uid()), pushMessage);
//			logger.info("推送评论消息返回结果Android:"+reAnd);
//			PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(comment.getTarget_uid()), pushMessage);
//			logger.info("推送评论消息返回结果IOS:"+reIOS);
//			if (null!=reAnd) {
//				msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
//				flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
//				flow.setMsgid(msgIdAnd);
//			}else if(null!=reIOS){
//				msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
//				flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
//				flow.setMsgid(msgIdIOS);
//			}else{
//				flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
//				flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
//			}
//			flow.setWorktype(2);
//			flow.setUid(comment.getUid());
//			flow.setOtheruid(lyrics.getUid());
		}
		
		flow.setWorkid(comment.getItemid());
		flow.setContent(comment.getComment());
		flow.setPushtype(YtConstant.PUSH_TYPE_COMMENT);
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, comment.getId());
		return result;
	}
	
	/**
	 * 
	 * delete:(删除评论). <br/> 
	 *
	 * @author panguixiang
	 * @param id
	 * @param type 1=歌曲，2=歌词
	 * @param itemid 作品id
	 * @param result
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public void delete(Long id,Integer type,Long itemid,Map<String, Object> result)
			throws Exception {
		workCommentMapper.deleteById(id);
		if(type==1){
			workMapper.addCommentNum(itemid,-1);//减去1
		}else if(type==2){
			lyricsMapper.addLrycCommentNum(itemid,-1);//减去1
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, YtConstant.SYS_SUCCSS_MSG);
	}
	
}
